package com.zlm.config;

import com.zlm.entity.User;
import com.zlm.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

@Component
public class MyUserDetialService implements UserDetailsService {
    // dao ===> springboot + mybatis

    private final UserMapper userMapper;

    @Autowired
    public MyUserDetialService(UserMapper userMapper) {
        this.userMapper = userMapper;
    }

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        // 返回用户
        User user = userMapper.loadUserByUsername(username);
        if(ObjectUtils.isEmpty(user))throw new RuntimeException("用户不存在");
        user.setRoles(userMapper.getRolesByUid(user.getId()));
        return user;
    }
}
